package com.cat.binaryTree;

import java.util.HashMap;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/remove-zero-sum-consecutive-nodes-from-linked-list/description/
 * @create 2025/9/28 16:48
 * @since JDK17
 */

public class Solution78 {
    public ListNode removeZeroSumSublists(ListNode head) {
        ListNode dummy = new ListNode(0, head);
        HashMap<Integer, ListNode> map = new HashMap<>();
        int sum = 0;
        for (var p = dummy; p != null; p = p.next) {
            sum += p.val;
            map.put(sum, p);
        }

        sum = 0;
        for (var p = dummy; p != null; p = p.next) {
            sum += p.val;
            p.next = map.get(sum).next;
        }

        return dummy.next;
    }
}
